﻿(function () {
    $(function () {
        
        var _$dataTable = $('#CodeGroupDataTable');
        var _dataService = abp.services.app.codeGroup;

        var _codeDataService = abp.services.app.code;
        //对应AppPermissions里面的Pages_BasicDataModule_CodeGroups_Create等变量,如果这个地方不定义，列表里面不会出现按钮
        var _permissions = {
            create: abp.auth.hasPermission('Pages.BasicDataModule.CodeGroups.Create'),
            edit: abp.auth.hasPermission('Pages.BasicDataModule.CodeGroups.Edit'),
            childCode: abp.auth.hasPermission('Pages.BasicDataModule.CodeGroups.ChildCode'),
            editCode: abp.auth.hasPermission('Pages.BasicDataModule.Codes.Edit'),
            'delete': abp.auth.hasPermission('Pages.BasicDataModule.Codes.Delete')
        };
        //新增/编辑代码组窗体定义
        var _createOrEditModal = new app.ModalManager({
            viewUrl: abp.appPath + 'BasicDataModule/CodeGroups/CreateOrEditModal',
            scriptUrl: abp.appPath + 'Areas/BasicDataModule/Views/CodeGroups/_CreateOrEditModal.js',
            modalClass: 'CreateOrEditModal'//类名跟_CreateOrEditModal.js文件里面的类名app.modals.CreateOrEditModal一致，就能调用模态窗口了
        });

        //新增/编辑代码窗体定义
        var _createOrEditModal_Code = new app.ModalManager({
            viewUrl: abp.appPath + 'BasicDataModule/Codes/CreateOrEditModal',
            scriptUrl: abp.appPath + 'Areas/BasicDataModule/Views/Codes/_CreateOrEditModal.js',
            modalClass: 'CreateOrEditModal'//类名跟_CreateOrEditModal.js文件里面的类名app.modals.CreateOrEditModal一致，就能调用模态窗口了
        });

        //代码组表格定义，包括样式，触发事件等。
        _$dataTable.jtable({

            //title: app.localize('Pages_BasicDataModule_Classrooms'),
            paging: true,
            sorting: true,
            multiSorting: true,

            actions: {
                listAction: {
                    method: _dataService.getObjects
                }
            },

            fields: {
                id: {
                    key: true,
                    list: false
                },
                actions: {
                    title: app.localize('Actions'),
                    width: '20%',
                    sorting: false,
                    list: _permissions.edit || _permissions.delete,
                    display: function (data) {
                        var $span = $('<span></span>');

                        if (_permissions.edit) {
                            $('<button class="btn btn-default btn-xs" title="' + app.localize('Edit') + '"><i class="fa fa-edit"></i></button>')
                                .appendTo($span)
                                .click(function () {
                                    _createOrEditModal.open({ id: data.record.id });
                                });
                        }

                        if (_permissions.childCode) {
                            $('<button class="btn btn-default btn-xs" title="' + app.localize('Pages_BasicDataModule_CodeGroups_ChildCode') + '"><i class="fa fa-th-list"></i></button>')
                                .appendTo($span)
                                .click(function () {
                                    //点击刷新代码列表
                                    //abp.ajax({
                                    //    data: JSON.stringify({
                                    //        codeGroupId:data.record.id
                                    //    })
                                    //});
                                    //alert(data.record.codeGroupName);
                                    dataTableCodes.load(data.record.id, data.record.codeGroupName);
                                });
                        }

                        return $span;
                    }
                },
                //网格里面字段必须驼峰写法
                codeGroupName: {
                    title: app.localize('Page_BasicDataModule_CodeGroups_GridColumn_Name'),
                    width: '30%'
                },
                
            }
            
        });//表格定义结束
        
        //代码表格
        var dataTableCodes = {
            $table: $('#CodeDataTable'),
            $emptyInfo: $('#CodeListEmptyInfo'),
            $selectedCodeGroupTitle: $('#SelectedCodeGroupTitle'),
            $selectedCodeGroupID: $('#SelectedCodeGroupID'),
            $addCodeButton: $('#AddCodeButton'),

            showTable: function (selectCodeGroupName,id) {
                dataTableCodes.$emptyInfo.hide();
                
                dataTableCodes.$table.show();
                dataTableCodes.$addCodeButton.show();
                dataTableCodes.$selectedCodeGroupTitle.text(': ' + selectCodeGroupName).show(); 
                dataTableCodes.$selectedCodeGroupID.text(id).hide();

            },
            hideTable: function () {
                dataTableCodes.$table.hide();
                dataTableCodes.$addCodeButton.hide();
                dataTableCodes.$selectedCodeGroupTitle.hide();
                dataTableCodes.$selectedCodeGroupID.hide();
                dataTableCodes.$emptyInfo.show();
            },
            load: function (selectCodeGroupId,selectCodeGroupName) {
                //如果没有点击子代码按钮，则执行hideTable函数
               
                if (selectCodeGroupId == null)
                {
                    dataTableCodes.hideTable();
                    return;
                }
               
                dataTableCodes.showTable(selectCodeGroupName, selectCodeGroupId);
                //加载数据方法
                dataTableCodes.$table.jtable('load', {
                    filter: selectCodeGroupId //过滤条件
                   
                });
            },
            init: function () {
                dataTableCodes.$table.jtable({
                    //title: app.localize('Users'),
                    paging: true,

                    actions: {
                        listAction: {
                            method: _codeDataService.getObjects
                        }
                    },

                    fields: {
                        actions: {
                            title: app.localize('Actions'),
                            width: '30%',
                            list: _permissions.manageMembers,
                            display: function (data) {
                                var $span = $('<span></span>');

                                if (_permissions.editCode) {
                                    $('<button class="btn btn-default btn-xs" title="' + app.localize('Edit') + '"><i class="fa fa-edit"></i></button>')
                                        .appendTo($span)
                                        .click(function () {
                                            //alert($('#SelectedCodeGroupID').text())
                                            _createOrEditModal_Code.open({ id: data.record.id, codeGroupId: $('#SelectedCodeGroupID').text() });
                                        });
                                }

                                if (_permissions.delete) {
                                    $('<button class="btn btn-default btn-xs" title="' + app.localize('Delete') + '"><i class="fa fa-trash-o"></i></button>')
                                        .appendTo($span)
                                        .click(function () {
                                            deleteObject(data.record);
                                        });
                                }

                                return $span;
                            }

                        },
                        codeName: {
                            title: app.localize('Page_BasicDataModule_Codes_GridColumn_Name'),
                            width: '40%'
                        },
                        codeKey: {
                            title: app.localize('Page_BasicDataModule_Codes_GridColumn_Value'),
                            width: '30%'
                        }
                        //creationTime: {
                        //    title: app.localize('AddedTime'),
                        //    width: '30%',
                        //    display: function (data) {
                        //        return moment(data.record.creationTime).format('L');
                        //    }
                        //}
                    }
                });

                //$('#AddUserToOuButton').click(function (e) {
                //    e.preventDefault();
                //    members.openAddModal();
                //});

                dataTableCodes.hideTable();
            }
        }
        //加载代码组数据方法
        function getObjects(reload) {
            if (reload) {
                _$dataTable.jtable('reload');
            } else {
                _$dataTable.jtable('load', {
                    filter:'' //如果没有查询条件输入框，此处置为空
                });
            }
        }
        //删除代码信息
        function deleteObject(o) {
            abp.message.confirm(
                app.localize('Com_Msg_Delete_Warning', o.codeName),
                function (isConfirmed) {
                    if (isConfirmed) {
                        _codeDataService.deleteObject({
                            id: o.id
                        }).done(function () {
                            dataTableCodes.load($('#SelectedCodeGroupID').text(), $('#SelectedCodeGroupTitle').text().replace(":", ""));
                            abp.notify.success(app.localize('SuccessfullyDeleted'));
                        });
                    }
                }
            );
        }
        //打开新增/编辑代码组页面
        $('#AddCodeGroupButton').click(function (e) {
            e.preventDefault();
            _createOrEditModal.open();
        });

        //打开新增/编辑代码页面
        $('#AddCodeButton').click(function (e) {
            e.preventDefault();
            //alert($('#SelectedCodeGroupID').text());
            //页面传参数
            _createOrEditModal_Code.open({ codeGroupId: $('#SelectedCodeGroupID').text() });
        });

        //新增/编辑保存之后回到主页面，重新加载主页面代码组数据。
        abp.event.on('app.CreateOrEditModalSaved', function () {
            getObjects(true);
        });
        ////新增/编辑保存之后回到主页面，重新加载主页面代码数据
        abp.event.on('app.CreateOrEditModalSavedCode', function () {
            //alert($('#SelectedCodeGroupTitle').text());
            dataTableCodes.load($('#SelectedCodeGroupID').text(), $('#SelectedCodeGroupTitle').text().replace(":",""));
        });
        dataTableCodes.init();
        //加载数据
        getObjects();
    });
})();